package com.itheima.controller;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
 * 日期控制器类
 */
@Controller
public class DateController {
	/**
	 * 使用自定义类型数据绑定日期数据
	 */
	@RequestMapping("/customDate")
	public String CustomDate(Date date) {
	    System.out.println("date="+date);
	    return "success";
	}
	
	@RequestMapping("/customDate2")
	public String CustomDate2(Date date,Model model) {
	    System.out.println("date="+date);
		model.addAttribute("datedata",date);
	    return "success_date";
	}
	
	@RequestMapping("/question")
	public String question(Model model) {
		Date dNow = new Date();
		model.addAttribute("datedata",new SimpleDateFormat("yyyy-MM-dd").format(dNow));
	    return "question";
	}
	
	@RequestMapping("/answer")
	public String answer(Integer i,Model model) throws ParseException {//@RequestParam(value="date")Date strDate
		Date dNow = new Date();
		SimpleDateFormat ft1 = new SimpleDateFormat("yyyy-MM-dd");
		System.out.println("当前日期为: " + ft1.format(dNow));
		SimpleDateFormat ft2 = new SimpleDateFormat("yyyy");
		//System.out.println("当前年为: " + ft2.format(dNow));
		Integer year = new Integer(ft2.format(dNow));
		SimpleDateFormat ft3 = new SimpleDateFormat("MM");
		//System.out.println("当前月为: " + ft3.format(dNow));
		Integer mon = new Integer(ft3.format(dNow));
		SimpleDateFormat ft4 = new SimpleDateFormat("dd");
		//System.out.println("当前日为: " + ft4.format(dNow));
		Integer day = new Integer(ft4.format(dNow));
		year=year+i;
		//System.out.println(year);
		String newdate = String.format(year+"-"+mon+"-"+day);
		System.out.println(i+"年后的日期为:"+newdate);
		Date dmil = new Date();
		dmil = ft1.parse(newdate);
		System.out.printf("本地星期的简称：%tA%n", dmil);
		String[] weekDays={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dmil);
        int w=cal.get(Calendar.DAY_OF_WEEK)-1;
        w=w<0?0:w;
        model.addAttribute("newdate",newdate);
		model.addAttribute("datedata",weekDays[w]);
	    return "answer";
	}
}
